//
//  DBHomeFastLoanCell.swift
//  DBCA
//
//  Created by mugua on 2024/7/4.
//

import Foundation

class DBHomeFastLoanCell: UITableViewCell {
    
    let disposeBag = DisposeBag()
    let buttonTap = PublishSubject<Void>()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func initUI() {
        contentView.addSubview(bgView)
        bgView.addSubview(fastLoanLogoImgView)
        bgView.addSubview(appNameLabel)
        bgView.addSubview(loanAmountLabel)
        bgView.addSubview(moneyLabel)
        bgView.addSubview(interestTermLabel)
        bgView.addSubview(lineView)
        bgView.addSubview(loanTermDayLabel)
        bgView.addSubview(btn)
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(15)
            make.bottom.equalToSuperview()
        }
        
        fastLoanLogoImgView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(15)
            make.width.height.equalTo(22)
        }
        
        appNameLabel.snp.makeConstraints { make in
            make.left.equalTo(38)
            make.centerY.equalTo(fastLoanLogoImgView)
            make.height.equalTo(15)
        }
        
        loanAmountLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(49)
            make.height.equalTo(15)
        }
        
        moneyLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(73)
            make.height.equalTo(20)
        }
        
        
        loanTermDayLabel.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalTo(fastLoanLogoImgView)
            make.height.equalTo(15)
        }
        
        lineView.snp.makeConstraints { make in
            make.right.equalTo(loanTermDayLabel.snp.left).offset(-5)
            make.top.equalTo(20)
            make.width.equalTo(1)
            make.height.equalTo(11)
        }
        
        interestTermLabel.snp.makeConstraints { make in
            make.right.equalTo(lineView.snp.left).offset(-5)
            make.centerY.equalTo(fastLoanLogoImgView)
            make.height.equalTo(15)
        }
        
        btn.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.top.equalTo(60)
            make.width.equalTo(100)
            make.height.equalTo(34)
        }
        
    }
    
    var model: DBHomeWorkinModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            fastLoanLogoImgView.kf.setImage(with: URL(string: model.wrestling ?? ""), placeholder: UIImage(named: "home_fast_loan_logo"))
            appNameLabel.text = model.nod
            moneyLabel.text = model.table
            loanAmountLabel.text = model.weaker
            btn.setTitle(model.indian, for: .normal)
            
            if let money = model.table {
                if money.contains("R") {
                    let tableArr = money.components(separatedBy: " ")
                    let att = NSMutableAttributedString(string: money)
                    att.addAttribute(.font, value: DBFuturaMaxiStd_DemiFont(14), range: tableArr.first!.rangeOfAll())
                    moneyLabel.attributedText = att
                }
            }
        }
    }
    
    
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = DBHexColor("F6F6F6")
        view.layer.cornerRadius = 2
        return view
    }()
    
    private lazy var fastLoanLogoImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_fast_loan_logo")
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    private lazy var appNameLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBTextColor
        label.font = DBFuturaMaxiStd_DemiFont(12)
        label.text = "Dana Bank"
        return label
    }()
    
    private lazy var moneyLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBTextColor
        label.font = DBFuturaMaxiStd_DemiFont(20)
        label.text = "9000,000"
        return label
    }()
    
    private lazy var loanAmountLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBHexColor("B1BEB5")
        label.font = DBFuturaMaxiStd_DemiFont(12)
        label.text = "Loan Amount"
        return label
    }()
    
    private lazy var interestTermLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBTextColor
        label.font = DBFuturaMaxiStd_DemiFont(14)
        label.text = "0.08%"
        return label
    }()
    
    private lazy var loanTermDayLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBTextColor
        label.font = DBFuturaMaxiStd_DemiFont(14)
        label.text = "91 hair"
        return label
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = DBHexColor("B1BEB5")
        return view
    }()
    
    private lazy var btn: UIButton = {
        let btn = UIButton()
        btn.clipsToBounds = true
        btn.delayTime = 1
        btn.backgroundColor = DBViewBgColor
        btn.titleLabel?.font = DBFuturaMaxiStd_DemiFont(12)
        btn.setTitle("Segra Ambil", for: .normal)
        btn.setTitleColor(DBHexColor("CCFF00"), for: .normal)
        btn.layer.cornerRadius = 2
        
        btn.rx
            .tap
            .bind(to: buttonTap)
            .disposed(by: disposeBag)
        return btn
    }()
    
    
    
}

